<?php
/**
 * 新闻公告管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-09-07
 */
namespace Admin\Controller; 

class NoticeController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db_sec   =D("Section");
        $this->db_notice=D("Notice");
    }
    
    /*
     * 查看、编辑前置操作
     * 
     * @return #
     */
    public function _before_show(){
        $list   =$this->db_sec->where(array("status"=>1))->order('id'.' '.asc)->select();
        $this->assign("list",$list);
    }
    
    /*
     * 编辑前置操作
     * 
     * @return #
     */
    public function edit(){
        if(I("request.id")){
            $map['id']          =I("request.id");
        }
        if(I("request.user_id")){
            $map['user_id']     =I("request.user_id");
        }else{
            $map['user_id']     =session("authId");
        }
        if(I("request.title")){
            $map['title']       =I("request.title");
        }else{
            $this->error("标题不能为空");
        }
        if(I("request.en_title")){
            $map['en_title']    =I("request.en_title");
        }
        if(I("request.top")){
            $top                =$this->db_notice->max("top");
            $map['top']         =$top+1;
        }
        if(I("request.content") && I("request.en_content")){
            $content            =str_replace('微软雅黑','Roboto',trim(I("request.content",'',"")));
            $en_content         =str_replace('微软雅黑','Roboto',trim(I("request.en_content",'',"")));
            $map['content']     =str_replace("</p","</div",str_replace("<p",'<div',$content));
            $map['en_content']  =str_replace("</p","</div",str_replace("<p",'<div',$en_content));
        }else{
            $this->error('公告内容不能为空');
        }
        if(I("request.section_id")){
           $map['section_id']   =I("request.section_id");
        }else{
           $map['section_id']   =1;
        }
        if(I("request.sign")){
            $map['sign']        =I("request.sign");
        }  
        if($map['id']){
            //id存在为修改            
            if($this->db_notice->save($map)){
                if(A("Common")->addLog("修改",$map['title'])){
                    $this->success("编辑成功");
                }else{
                    $this->error("编辑失败");
                }
            }
        }else{
            //id不存在，新增
            $top        =$this->db_notice->max("top");
            $map['top'] =$top+1;
            if($this->db_notice->add($map)){
                if(A("Common")->addLog("新增",$map['title'])){
                    $this->success("新增成功");
                }else{
                    $this->error("新增失败");
                }
            }
        }
    }
}